<!doctype html>

<html>

<head>

<meta charset="utf-8">

<meta name="apple-itunes-app" content="app-id=468944728">

<meta id="viewport" name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no">

<title>授权页</title>

<head>

<script type="text/javascript" src="js/jquery.min.js"></script>

<script>



var appid = '';

var secret = '';

var weixinId = 1;

var param = getUrlParam("param");
//var strs = new Array(); //定义一数组
//strs = param.split("-"); //字符分割

//0 类型 1用户Id 2 邀请Id 3限制id
//if(strs.length>0) {
    var type = 0;
   // var memberId = strs[1]; //
   //var refId = strs[2];
  //  var limitId = strs[3];
//}


var url_ = 'http://weixin.zjpxny.com/authorizationPage.html?param='+param;

var redirect_uri = encodeURI(url_);

var code = '';


	//appid = jQuery.trim(data.appId);
    appid = 'wx5a7259af417cf2e9';

	//secret = jQuery.trim(data.appSecret);
    secret = '04c36859319b12a1643bf3e5533a8572';

	code = getUrlParam("code");
	if(code==null){
		window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
	}else{

		//alert('appid:'+appid+',secret:'+secret+',code:'+code+',grant_type:authorization_code');
		//获取当前微信用户信息

		$.ajax({

			type:'post',

			dataType:'json',

			url:'http://mobile.zjpxny.com/mobile/weixin/getWxUserInfo',

			data:{'type':type,'recId':0,'appid':appid,'secret':secret,'code':code,'grant_type':'authorization_code'},

			async:false,

			success:function(data){

              //  alert(data.result)
				var result = JSON.parse(data.result);
                console.log(data.openid)
                var openid = result.unionid;
                var oid=result.openid;
				if(result.errcode == 48001){

					window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

				}else{

	          		if(result.openid == undefined || result.openid == 'undefined'){

	          			window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

	          		}else{

	          			//alert("shopId="+shopId+",externalNo="+result.openid+",externalName="+result.nickname+",logo="+result.headimgurl+',shopId='+shopId);

	          			$.ajax({

		          			type:'post',

		          			dataType:'json',

		          			url:"http://mobile.zjpxny.com/mobile/weixin/getMemberByOpenId",

		          			data:{'openId':result.unionid,'userId':0},

		          			async:false,

		          			success:function(data){
		          				if(data!=null){

		          				    console.log(data);
		          				    //绑定手机

									if(data.mobile == '' || data.mobile == null) {
									  //  alert(data.mobile)
                                        alert("绑定手机1")

										window.location.href = 'http://weixin.zjpxny.com/bind.html?openId='+openid+'&oid='+oid;
                                        //?memberId='+data.id+'&type='+type+'&productId='+refId+'&limitId='+limitId;
									} else {


                                    //    alert("11")
                                     //   alert(open)
                                        $.ajax({
                                            type: "post",
                                            url: "http://mobile.zjpxny.com"+ '/mobile/weixin/updateOpenid',
                                            async: true,
                                            //productId=412&memberId=623&normalId=508&num=1
                                            data: {'unionid': openid, 'openid': oid},
                                            success: function (data) {
                                            //    alert(data);
                                            }
                                           // f
                                        });

                                        localStorage.setItem('memberId',data.id);
                                        sessionStorage.setItem('memberId', getUrlParam('memberId'));
                                        window.location.href = "/index.html?memberId="+data.id;
                                     //   window.location.href = "/index.html";
                                        // }
									}
						        }else{
                                    alert("绑定手机2")
//		          				    alert('会员不存在!');
		          				    //会员不存在
                                    window.location.href = 'http://weixin.zjpxny.com/bind.html?openId='+openid+'&oid='+oid;

						        }

		          				

		          			},

		          			error:function(){
                                alert("绑定手机3")
		//			           alert('会员不存在2!');
                                 window.location.href = 'http://weixin.zjpxny.com/bind.html?openId='+openid+'&oid='+oid;
					         }

		          		});

	          		}

	          		

	          	}

          		

			},

			error:function(){

				alert('系统异常3');

			}

			

		})

	}






//地址栏获取参数

function getUrlParam(name){

	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 

	var r = window.location.search.substr(1).match(reg);  

	if (r!=null) 

		return unescape(r[2]); 

	return null; 

} 

</script>

</head>

<body></body>

</html>